From a77149f8e43f254cc6a0ac87271fd1a31b2a9474 Mon Sep 17 00:00:00 2001 From: "emellor@leeni.uk.xensource.com" Date: Wed, 19 Oct 2005 16:24:54 +0100 Subject: [PATCH] New network-bridge script and associated gubbins. This is Kurt Garloff's reworked network-bridge script: * we got rid of ifconfig * it works for netdev != eth0 * arp on and off are symmetric as are ifdown and ifup * ifup will be passed the ifcfg config file name if needed (the ifup may otherwise figure that the veth0 hardware is NOT the same as the original ${netdev} and not use the same config -- this happens on SUSE. Charles Coffing tracked this one down.) Plus Kurt's avoid-dash patch: the network setup scripts on SUSE have trouble with the bridge name xen-br0; they don't expect the '-'. Arguably this should be fixed. But I assume there's more scripts out there which may not like it, so I suggest the following patch to rename xen-br0 to xenbr0. Plus Charles Duffy's patch to support multiple bridges: The attached patch allows the network-bridge script to be used to generate multiple bridges corresponding to different physical interfaces. It adds a new parameter, "vifnum", used to refer both to the loopback interface to be used and to set defaults regarding the physical interface and bridge name. Thus, if one wishes to start xenbr0 on eth0 and xenbr1 on eth1, one need only call: network-bridge start ## vifnum is 0 by default network-bridge start vifnum=1 ...well, that and set loopback.nloopbacks=2 in the Dom0 kernel parameters. Plus renaming of virtnum to vifnum in Charles' patch, as requested by Ian Pratt. Plus a fix to DevController to allocate vif IDs starting from 0 (i.e. vif2.0 is now domain 2's first vif, as opposed to vif2.1 in the recent past). Plus tidying up inside network-bridge using some helper variables. Signed-off-by: Ewan Mellor --- tools/examples/network-bridge | 201 +++++++++++------- tools/examples/vif-bridge | 2 +- tools/examples/xend-config.sxp | 2 +- tools/examples/xmexample.vmx | 2 +- tools/examples/xmexample1 | 2 +- tools/examples/xmexample2 | 2 +- tools/ioemu/target-i386-dm/qemu-ifup | 2 +- tools/misc/netfix | 4 +- tools/python/xen/xend/XendRoot.py | 2 +- tools/python/xen/xend/server/DevController.py | 2 +- tools/python/xen/xend/server/SrvDaemon.py | 47 +--- tools/vnet/vnet-module/varp.c | 2 +- 12 files changed, 142 insertions(+), 128 deletions(-) mode change 100755 => 100644 tools/examples/network-bridge diff --git a/tools/examples/network-bridge b/tools/examples/network-bridge old mode 100755 new mode 100644 index f0e1e1648d..c6fb1c1f47 --- a/tools/examples/network-bridge +++ b/tools/examples/network-bridge @@ -5,8 +5,8 @@ # The script name to use is defined in /etc/xen/xend-config.sxp # in the network-script field. # -# This script creates a bridge (default xen-br0), adds a device -# (default eth0) to it, copies the IP addresses from the device +# This script creates a bridge (default xenbr${vifnum}), adds a device +# (default eth${vifnum}) to it, copies the IP addresses from the device # to the bridge and adjusts the routes accordingly. # # If all goes well, this should ensure that networking stays up. @@ -20,9 +20,12 @@ # # Vars: # -# bridge The bridge to use (default xen-br0). -# netdev The interface to add to the bridge (default eth0). -# antispoof Whether to use iptables to prevent spoofing (default yes). +# vifnum Virtual device number to use (default 0). Numbers >=1 +# require the netback driver to have nloopbacks set to a +# higher value than its default of 1. +# bridge The bridge to use (default xenbr${vifnum}). +# netdev The interface to add to the bridge (default eth${vifnum}). +# antispoof Whether to use iptables to prevent spoofing (default no). # # start: # Creates the bridge and enslaves netdev to it. @@ -60,11 +63,37 @@ shift # Pull variables in args in to environment. for arg ; do export "${arg}" ; done -bridge=${bridge:-xen-br0} -netdev=${netdev:-eth0} +vifnum=${vifnum:-0} +bridge=${bridge:-xenbr${vifnum}} +netdev=${netdev:-eth${vifnum}} antispoof=${antispoof:-no} -echo "*network $OP bridge=$bridge netdev=$netdev antispoof=$antispoof" >&2 +pdev="p${netdev}" +vdev="veth${vifnum}" +vif0="vif0.${vifnum}" + +echo "*network $OP bridge=$bridge netdev=$netdev antispoof=$antispoof vifnum=$vifnum" >&2 + +legacy_mask_to_prefix() { + mask=$1 + first=${mask%%.*} + second=${mask#*.} + third=${second#*.} + fourth=${third#*.} + second=${second%%.*} + third=${third%%.*} + declare -i INT FULLMASK BIT + INT=$((((($first*256)+$second)*256+$third)*256+$fourth)) + FULLMASK=4294967295 + BIT=1 + for bit in `seq 32 -1 0`; do + if test $FULLMASK -eq $INT; then PREFIX=$bit; return; fi + FULLMASK=$(($FULLMASK-$BIT)) + BIT=$((BIT*2)) + done + echo "ERROR converting netmask $mask to prefix" + exit 1 +} # Usage: transfer_addrs src dst # Copy all IP addresses (including aliases) from device $src to device $dst. @@ -99,6 +128,7 @@ s/inet/ip addr del/ s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)/[0-9]\+@\1@ s/${src}/dev ${src}/ " | sh -e + ip link set dev ${dst} up } # Usage: transfer_routes src dst @@ -131,11 +161,11 @@ create_bridge () { # Don't create the bridge if it already exists. if ! brctl show | grep -q ${bridge} ; then - brctl addbr ${bridge} - brctl stp ${bridge} off - brctl setfd ${bridge} 0 + brctl addbr ${bridge} + brctl stp ${bridge} off + brctl setfd ${bridge} 0 fi - ifconfig ${bridge} up + ip link set ${bridge} up } # Usage: add_to_bridge bridge dev @@ -144,7 +174,7 @@ add_to_bridge () { local dev=$2 # Don't add $dev to $bridge if it's already on a bridge. if ! brctl show | grep -q ${dev} ; then - brctl addif ${bridge} ${dev} + brctl addif ${bridge} ${dev} fi } @@ -166,8 +196,10 @@ show_status () { local bridge=$2 echo '============================================================' - ifconfig ${dev} - ifconfig ${bridge} + ip addr show ${dev} + ip addr show ${bridge} + echo ' ' + brctl show ${bridge} echo ' ' ip route list echo ' ' @@ -177,100 +209,113 @@ show_status () { op_start () { if [ "${bridge}" == "null" ] ; then - return + return fi create_bridge ${bridge} - if ifconfig 2>/dev/null | grep -q veth0 ; then - return + if ! ip link show 2>/dev/null | grep -q "^[0-9]*: ${vdev}"; then + return fi - if ifconfig veth0 2>/dev/null | grep -q veth0 ; then - mac=`ifconfig ${netdev} | grep HWadd | sed -e 's/.*\(..:..:..:..:..:..\).*/\1/'` - if ! ifdown ${netdev} ; then - # if ifup didn't work, see if we have an ip= on cmd line - if egrep 'ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:' /proc/cmdline ; - then - kip=`sed -e 's!.*ip=\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\):.*!\1!' /proc/cmdline` - kmask=`sed -e 's!.*ip=[^:]*:[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline` - kgate=`sed -e 's!.*ip=[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline` - ifconfig ${netdev} 0.0.0.0 down - fi + if ip link show ${vdev} 2>/dev/null >/dev/null; then + mac=`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether \(..:..:..:..:..:..\).*/\1/'` + eval `/sbin/getcfg -d /etc/sysconfig/network/ -f ifcfg- -- ${netdev}` + transfer_addrs ${netdev} ${vdev} + if ! ifdown ${netdev}; then + # if ifup didn't work, see if we have an ip= on cmd line + if egrep 'ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:' /proc/cmdline; then + kip=`sed -e 's!.*ip=\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\):.*!\1!' /proc/cmdline` + kmask=`sed -e 's!.*ip=[^:]*:[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline` + kgate=`sed -e 's!.*ip=[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline` + ip link set ${netdev} down + ip addr flush ${netdev} + fi fi - ip link set ${netdev} name p${netdev} - ip link set veth0 name ${netdev} - ifconfig p${netdev} 0.0.0.0 -arp down - ifconfig p${netdev} hw ether fe:ff:ff:ff:ff:ff - ifconfig ${netdev} hw ether ${mac} - add_to_bridge ${bridge} vif0.0 - add_to_bridge ${bridge} p${netdev} + ip link set ${netdev} name ${pdev} + ip link set ${vdev} name ${netdev} + ip link set ${pdev} down arp off + ip link set ${pdev} addr fe:ff:ff:ff:ff:ff + ip addr flush ${pdev} + ip link set ${netdev} addr ${mac} arp on + add_to_bridge ${bridge} ${vif0} + add_to_bridge ${bridge} ${pdev} ip link set ${bridge} up - ip link set vif0.0 up - ip link set p${netdev} up - if ! ifup ${netdev} ; then - if [ ${kip} ] ; then - # use the addresses we grocked from /proc/cmdline - ifconfig ${netdev} ${kip} - [ ${kmask} ] && ifconfig ${netdev} netmask ${kmask} - ifconfig ${netdev} up - [ ${kgate} ] && ip route add default via ${kgate} + ip link set ${vif0} up + ip link set ${pdev} up + if ! ifup ${HWD_CONFIG_0} ${netdev} ; then + if [ ${kip} ] ; then + # use the addresses we grocked from /proc/cmdline + if [ -z "${kmask}" ]; then + PREFIX=32; + else + legacy_to_prefix ${kmask}; fi - fi + ip addr add ${kip}/${PREFIX} dev ${netdev} + ip link set dev ${netdev} up + [ ${kgate} ] && ip route add default via ${kgate} + fi + fi else - # old style without veth0 - transfer_addrs ${netdev} ${bridge} - transfer_routes ${netdev} ${bridge} + # old style without ${vdev} + transfer_addrs ${netdev} ${bridge} + transfer_routes ${netdev} ${bridge} fi - + if [ ${antispoof} == 'yes' ] ; then - antispoofing ${netdev} ${bridge} + antispoofing ${netdev} ${bridge} fi } op_stop () { - if [ "${bridge}" == "null" ] ; then - return + if [ "${bridge}" == "null" ]; then + return + fi + if ! ip link show ${bridge} >/dev/null 2>&1; then + return fi - if ifconfig peth0 2>/dev/null | grep -q peth0 ; then - - ifconfig vif0.0 down - mac=`ifconfig eth0 | grep HWadd | \ - sed -e 's/.*\(..:..:..:..:..:..\).*/\1/'` - ifconfig ${netdev} 0.0.0.0 down - ifconfig ${netdev} hw ether fe:ff:ff:ff:ff:ff + if ip link show ${pdev} 2>/dev/null >/dev/null; then + ip link set dev ${vif0} down + mac=`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether \(..:..:..:..:..:..\).*/\1/'` + transfer_addrs ${netdev} ${pdev} + ifdown ${netdev} + ip link set ${netdev} down arp off + ip link set ${netdev} addr fe:ff:ff:ff:ff:ff + ip link set ${pdev} down + ip addr flush ${netdev} + ip link set ${pdev} addr ${mac} arp on - ifconfig p${netdev} down - ifconfig p${netdev} hw ether ${mac} arp - brctl delif ${bridge} p${netdev} + brctl delif ${bridge} ${pdev} + brctl delif ${bridge} ${vif0} + ip link set ${bridge} down - ip link set eth0 name veth0 - ip link set peth0 name eth0 - ifconfig ${bridge} down - brctl delbr ${bridge} - ifup eth0 + ip link set ${netdev} name ${vdev} + ip link set ${pdev} name ${netdev} + ifup ${netdev} else - transfer_routes ${bridge} ${netdev} + transfer_routes ${bridge} ${netdev} + ip link set ${bridge} down fi + brctl delbr ${bridge} } case ${OP} in start) - op_start - ;; + op_start + ;; stop) - op_stop - ;; + op_stop + ;; status) - show_status ${netdev} ${bridge} - ;; + show_status ${netdev} ${bridge} + ;; *) - echo 'Unknown command: ' ${OP} >&2 - echo 'Valid commands are: start, stop, status' >&2 - exit 1 + echo 'Unknown command: ' ${OP} >&2 + echo 'Valid commands are: start, stop, status' >&2 + exit 1 esac diff --git a/tools/examples/vif-bridge b/tools/examples/vif-bridge index 0174d81116..4a947397f4 100755 --- a/tools/examples/vif-bridge +++ b/tools/examples/vif-bridge @@ -8,7 +8,7 @@ # # Example invocation: # -# vif-bridge up domain=VM1 vif=vif1.0 bridge=xen-br0 ip="128.232.38.45/28 10.10.10.55/24" +# vif-bridge up domain=VM1 vif=vif1.0 bridge=xenbr0 ip="128.232.38.45/28 10.10.10.55/24" # # # Usage: diff --git a/tools/examples/xend-config.sxp b/tools/examples/xend-config.sxp index 75152f21db..881eaacfbc 100644 --- a/tools/examples/xend-config.sxp +++ b/tools/examples/xend-config.sxp @@ -33,7 +33,7 @@ # The script used to start/stop networking for xend. (network-script network-bridge) # The default bridge that virtual interfaces should be connected to. -(vif-bridge xen-br0) +(vif-bridge xenbr0) # The default script used to control virtual interfaces. (vif-script vif-bridge) diff --git a/tools/examples/xmexample.vmx b/tools/examples/xmexample.vmx index a15ad321a9..f9665ce67d 100644 --- a/tools/examples/xmexample.vmx +++ b/tools/examples/xmexample.vmx @@ -35,7 +35,7 @@ vcpus=1 # Optionally define mac and/or bridge for the network interfaces. # Random MACs are assigned if not given. -#vif = [ 'mac=aa:00:00:00:00:11, bridge=xen-br0' ] +#vif = [ 'mac=aa:00:00:00:00:11, bridge=xenbr0' ] #---------------------------------------------------------------------------- # Define the disk devices you want the domain to have access to, and diff --git a/tools/examples/xmexample1 b/tools/examples/xmexample1 index be3af30a95..cd73c7c3fa 100644 --- a/tools/examples/xmexample1 +++ b/tools/examples/xmexample1 @@ -36,7 +36,7 @@ name = "ExampleDomain" # Optionally define mac and/or bridge for the network interfaces. # Random MACs are assigned if not given. -#vif = [ 'mac=aa:00:00:00:00:11, bridge=xen-br0' ] +#vif = [ 'mac=aa:00:00:00:00:11, bridge=xenbr0' ] #---------------------------------------------------------------------------- # Define the disk devices you want the domain to have access to, and diff --git a/tools/examples/xmexample2 b/tools/examples/xmexample2 index 8272e84ab5..ba89eef7eb 100644 --- a/tools/examples/xmexample2 +++ b/tools/examples/xmexample2 @@ -67,7 +67,7 @@ vcpus = 4 # make your domain a 4-way # Optionally define mac and/or bridge for the network interfaces. # Random MACs are assigned if not given. -#vif = [ 'mac=aa:00:00:00:00:11, bridge=xen-br0' ] +#vif = [ 'mac=aa:00:00:00:00:11, bridge=xenbr0' ] #---------------------------------------------------------------------------- # Define the disk devices you want the domain to have access to, and diff --git a/tools/ioemu/target-i386-dm/qemu-ifup b/tools/ioemu/target-i386-dm/qemu-ifup index 87bff3410c..a23f1d2b68 100755 --- a/tools/ioemu/target-i386-dm/qemu-ifup +++ b/tools/ioemu/target-i386-dm/qemu-ifup @@ -7,4 +7,4 @@ echo 'config qemu network with xen bridge for ' echo $* ifconfig $1 0.0.0.0 up -brctl addif xen-br0 $1 +brctl addif xenbr0 $1 diff --git a/tools/misc/netfix b/tools/misc/netfix index e18923f132..669a95497a 100644 --- a/tools/misc/netfix +++ b/tools/misc/netfix @@ -3,7 +3,7 @@ #============================================================================ # Copyright (C) 2004 Mike Wray #============================================================================ -# Move the IP address from eth0 onto the Xen bridge (xen-br0). +# Move the IP address from eth0 onto the Xen bridge (xenbr0). # Only works if the bridge control utils (brctl) have been installed. #============================================================================ @@ -19,7 +19,7 @@ long_options = ['help', 'verbose', 'quiet', 'interface=', 'bridge=', 'create'] defaults['interface'] = 'eth0' -defaults['bridge'] = 'xen-br0' +defaults['bridge'] = 'xenbr0' def usage(): print """Usage: diff --git a/tools/python/xen/xend/XendRoot.py b/tools/python/xen/xend/XendRoot.py index e88e8ff4b3..d5d348d94b 100644 --- a/tools/python/xen/xend/XendRoot.py +++ b/tools/python/xen/xend/XendRoot.py @@ -253,7 +253,7 @@ class XendRoot: return self.get_config_bool('enable-dump', 'no') def get_vif_bridge(self): - return self.get_config_value('vif-bridge', 'xen-br0') + return self.get_config_value('vif-bridge', 'xenbr0') def get_vif_script(self): return self.get_config_value('vif-script', 'vif-bridge') diff --git a/tools/python/xen/xend/server/DevController.py b/tools/python/xen/xend/server/DevController.py index 8141e890b1..0060a687fd 100644 --- a/tools/python/xen/xend/server/DevController.py +++ b/tools/python/xen/xend/server/DevController.py @@ -160,7 +160,7 @@ class DevController: if result: result = int(result) else: - result = 1 + result = 0 t.write("nextDeviceID", str(result + 1)) if t.commit(): return result diff --git a/tools/python/xen/xend/server/SrvDaemon.py b/tools/python/xen/xend/server/SrvDaemon.py index 543502ad95..2bfcce85a7 100644 --- a/tools/python/xen/xend/server/SrvDaemon.py +++ b/tools/python/xen/xend/server/SrvDaemon.py @@ -32,34 +32,6 @@ class Daemon: self.traceindent = 0 self.child = 0 - def daemon_pids(self): - pids = [] - pidex = '(?P\d+)' - pythonex = '(?P\S*python\S*)' - cmdex = '(?P.*)' - procre = re.compile('^\s*' + pidex + '\s*' + pythonex + '\s*' + cmdex + '$') - xendre = re.compile('^\S+/xend\s*(start|restart)\s*.*$') - procs = os.popen('ps -e -o pid,args 2>/dev/null') - for proc in procs: - pm = procre.match(proc) - if not pm: continue - xm = xendre.match(pm.group('cmd')) - if not xm: continue - pids.append(int(pm.group('pid'))) - return pids - - def new_cleanup(self, kill=0): - err = 0 - pids = self.daemon_pids() - if kill: - for pid in pids: - print "Killing daemon pid=%d" % pid - os.kill(pid, signal.SIGHUP) - elif pids: - err = 1 - print "Daemon already running: ", pids - return err - def read_pid(self, pidfile): """Read process id from a file. @@ -115,12 +87,9 @@ class Daemon: os.remove(pidfile) return running - def cleanup_xend(self, kill=False): + def cleanup_xend(self, kill): return self.cleanup_process(XEND_PID_FILE, "xend", kill) - def cleanup(self, kill=False): - self.cleanup_xend(kill=kill) - def status(self): """Returns the status of the xend daemon. The return value is defined by the LSB: @@ -156,16 +125,13 @@ class Daemon: # Detach from TTY. os.setsid() - # Detach from standard file descriptors. - # I do this at the file-descriptor level: the overlying Python file - # objects also use fd's 0, 1 and 2. + # Detach from standard file descriptors, and redirect them to + # /dev/null or the log as appropriate. os.close(0) os.close(1) os.close(2) if XEND_DEBUG: os.open('/dev/null', os.O_RDONLY) - # XXX KAF: Why doesn't this capture output from C extensions that - # fprintf(stdout) or fprintf(stderr) ?? os.open(XEND_DEBUG_LOG, os.O_WRONLY|os.O_CREAT) os.dup(1) else: @@ -180,7 +146,7 @@ class Daemon: 0 Success 4 Insufficient privileges """ - xend_pid = self.cleanup_xend() + xend_pid = self.cleanup_xend(False) if self.set_user(): return 4 @@ -294,7 +260,10 @@ class Daemon: return 1 def stop(self): - return self.cleanup(kill=True) + result = self.cleanup_xend(True) + from xen.xend import Vifctl + Vifctl.network("stop") + return result def run(self, status): try: diff --git a/tools/vnet/vnet-module/varp.c b/tools/vnet/vnet-module/varp.c index 5de3d33254..012e29cea8 100644 --- a/tools/vnet/vnet-module/varp.c +++ b/tools/vnet/vnet-module/varp.c @@ -176,7 +176,7 @@ u32 varp_mcast_addr = 0; /** UDP port (network order). */ u16 varp_port = 0; -char *varp_device = "xen-br0"; +char *varp_device = "xenbr0"; #define VarpTable_read_lock(z, flags) do{ (flags) = 0; down(&(z)->lock); } while(0) #define VarpTable_read_unlock(z, flags) do{ (flags) = 0; up(&(z)->lock); } while(0) -- 2.30.2